package cn.ffcs.base.web;

import java.io.Serializable;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class CriteriaQuery implements Serializable
{
    public CriteriaQuery(){   
        super();   
    }   
    private static final long serialVersionUID = -1464383406564081554L;   
    // 初始化list   
    private String curPage=null ;   
    private int pageSize=10;   
    private String myaction;   
    private String myform;   
    private CriterionList criterionList;   
    private DetachedCriteria detachedCriteria;   
   
    public CriteriaQuery(Class c) {   
        this.criterionList = new CriterionList();   
        this.detachedCriteria=DetachedCriteria.forClass(c);   
    }   
    public CriteriaQuery(Class c,String curPage,String myaction,String myform) {   
        this.myaction=myaction;   
        this.myform=myform;   
        this.curPage=curPage;   
        this.criterionList = new CriterionList();   
        this.detachedCriteria=DetachedCriteria.forClass(c);   
    }   
    public CriteriaQuery(Class c,String curPage,String myaction) {   
        this.myaction=myaction;   
        this.curPage=curPage;   
        this.criterionList = new CriterionList();   
        this.detachedCriteria=DetachedCriteria.forClass(c);   
    }   
    public CriterionList getCriterionList() {   
        return criterionList;   
    }   
    public void setCriterionList(CriterionList criterionList) {   
        this.criterionList = criterionList;   
    }   
   
    public String getCurPage() {   
        return curPage;   
    }   
    public void setCurPage(String curPage) {   
        this.curPage = curPage;   
    }   
    public DetachedCriteria getDetachedCriteria() {   
        return detachedCriteria;   
    }   
    public void setDetachedCriteria(DetachedCriteria detachedCriteria) {   
        this.detachedCriteria = detachedCriteria;   
    }   
    public void add(Criterion c) {   
            detachedCriteria.add(c);   
    }   
    public void add() {   
        for(int i=0;i<getCriterionList().size();i++){
            add(getCriterionList().getParas(i));   
        }   
    }   
    public void createAlias(String name){   
        detachedCriteria.createCriteria(name);   
    }   
    public void createAlias(String name,String value){   
        detachedCriteria.createCriteria(name,value);   
    }   
    public Criterion and(CriteriaQuery query, int source, int dest) {   
        return Restrictions.and(query.getCriterionList().getParas(source),query.getCriterionList().getParas(dest));   
    }   
    public Criterion and(Criterion c,CriteriaQuery query, int source) {   
        return Restrictions.and(c, query.getCriterionList().getParas(source));   
    }   
    public Criterion and(Criterion c1, Criterion c2) {   
        return Restrictions.and(c1,c2);   
    }   
    public Criterion or(CriteriaQuery query, int source, int dest) {   
        return Restrictions.or(query.getCriterionList().getParas(source),query.getCriterionList().getParas(dest));   
    }   
       
    public Criterion or(Criterion c,CriteriaQuery query, int source) {   
        return Restrictions.or(c, query.getCriterionList().getParas(source));   
    }   
       
    public void or(Criterion c1, Criterion c2) {   
        this.detachedCriteria.add(Restrictions.or(c1,c2));   
    }   
    public void addOrder(String ordername,String ordervalue) {   
        // 写入order查询条件   
        if ("asc".equals(ordername)) {   
            detachedCriteria.addOrder(Order.asc(ordervalue));   
        } else {   
            detachedCriteria.addOrder(Order.desc(ordervalue));   
        }   
    }   
    public void eq(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.eq(keyname, keyvalue));   
    }   
    public void notEq(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.or(Restrictions.gt(keyname, keyvalue),Restrictions.lt(keyname, keyvalue)));   
    }   
    public void like(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.like(keyname, keyvalue));   
    }   
    public void gt(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.gt(keyname, keyvalue));   
    }   
    public void lt(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.lt(keyname, keyvalue));   
    }   
    public void le(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.le(keyname, keyvalue));   
    }   
    public void ge(String keyname,Object keyvalue){   
        criterionList.addPara(Restrictions.ge(keyname, keyvalue));   
    }   
    public void ilike(String keyname,Object[] keyvalue){   
        criterionList.addPara(Restrictions.in(keyname, keyvalue));   
    }   
    public void between(String keyname,Object keyvalue1,Object keyvalue2) {   
        Criterion c=null;// 写入between查询条件   
//        if (!AppUtils.isBlank(keyvalue1) && !AppUtils.isBlank(keyvalue2)) {   
//            c=Restrictions.between(keyname, keyvalue1, keyvalue2);   
//        } else if (!AppUtils.isBlank(keyvalue1)) {   
//            c=Restrictions.ge(keyname, keyvalue1);   
//        } else if (!AppUtils.isBlank(keyvalue2)) {   
//            c=Restrictions.le(keyname, keyvalue2);   
//        }   
        criterionList.add(c);   
    }   
    public int getPageSize() {   
        return pageSize;   
    }   
    public void setPageSize(int pageSize) {   
        this.pageSize = pageSize;   
    }   
    public String getMyaction() {   
        return myaction;   
    }   
    public void setMyaction(String myaction) {   
        this.myaction = myaction;   
    }   
    public String getMyform() {   
        return myform;   
    }   
    public void setMyform(String myform) {   
        this.myform = myform;   
    }   
}
